﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System.Collections.Generic;
using System.Windows.Forms;

namespace TeachMe.Application.Configuration
{
    public class ListViewColumnsProportions
    {
        public class ColumnData
        {
            public string Name { get; set; }
            public int Width { get; set; }
        }

        public string ParentFormName { get; set; }
        public string Name { get; set; }
        public List<ColumnData> Columns { get; set; }

        public static void Lock(Form parentForm, ListView listView, MachineSettings machineSettings)
        {
            foreach (ListViewColumnsProportions item in machineSettings.ColumnsProportions)
            {
                if (item.ParentFormName == parentForm.Name && item.Name == listView.Name)
                {
                    foreach (ColumnHeader columnHeader in listView.Columns)
                    {
                        foreach (ColumnData columnData in item.Columns)
                        {
                            if (columnData.Name == columnHeader.Text)
                            {
                                columnHeader.Width = columnData.Width;

                                break;
                            }
                        }
                    }

                    break;
                }
            }

            parentForm.Closing += delegate
                                     {
                                         ListViewColumnsProportions listViewColumnsProportions = null;

                                         foreach (ListViewColumnsProportions item in machineSettings.ColumnsProportions)
                                         {
                                             if (item.ParentFormName == parentForm.Name && item.Name == listView.Name)
                                             {
                                                 listViewColumnsProportions = item;
                                             }
                                         }

                                         using (machineSettings.Edit())
                                         {
                                             if (listViewColumnsProportions == null)
                                             {
                                                 listViewColumnsProportions = new ListViewColumnsProportions();
                                                 listViewColumnsProportions.ParentFormName = parentForm.Name;
                                                 listViewColumnsProportions.Name = listView.Name;
                                                 listViewColumnsProportions.Columns = new List<ColumnData>();

                                                 machineSettings.ColumnsProportions.Add(listViewColumnsProportions);
                                             }

                                             listViewColumnsProportions.Columns.Clear();

                                             foreach (ColumnHeader columnHeader in listView.Columns)
                                             {
                                                 listViewColumnsProportions.Columns.Add(new ColumnData { Name = columnHeader.Text, Width = columnHeader.Width });
                                             }
                                         }
                                     };
        }
    }
}
